package team.yidong.fenda.controller;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import team.yidong.fenda.entity.Hospital;
import team.yidong.fenda.entity.HospitalImg;
import team.yidong.fenda.service.HospitalImgService;
import team.yidong.fenda.service.HospitalService;
import team.yidong.fenda.utils.ImageCheck;
import team.yidong.fenda.utils.QiNiuYunUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

/**
 * Created by xuanlong on 2017/10/11.
 */


@Controller
public class ImagesController {
    @Autowired
    HospitalService hospitalService;
    @Autowired
    ImageCheck imageCheck;
    @Autowired
    QiNiuYunUtil qiNiuYunUtil;
    @Autowired
    HospitalImgService hospitalImgService;

    @RequestMapping("/images")
    public String images(Model model, HttpSession session) {
        Integer hospitalId = (Integer) session.getAttribute("loginHospitalId");
        String sysName = (String) session.getAttribute("loginSysUserName");
        model.addAttribute("sysName", sysName);

        Hospital hospital = hospitalService.findHospitalById(hospitalId);
        model.addAttribute("hospital", hospital);
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.setExcludes(new String[]{"hospital", "hospitalPermissionSet","userSet", "masterList"});
        model.addAttribute("hospitalImg", JSONArray.fromObject(hospitalImgService.findByHospital(hospitalId), jsonConfig));
        if (hospitalId != 1) {


            model.addAttribute("hospitals", JSONArray.fromObject(hospitalService.findHospitalById(hospitalId), jsonConfig));
        } else {
            JSONArray jsonArray = hospitalService.findAll();
            jsonArray.remove(0);
            model.addAttribute("hospitals", jsonArray);
        }
        return "images";
    }

    @RequestMapping("/searchImg")

    public String searchImges(Model model, HttpSession session, @RequestParam String hospitalName) {
        Integer hospitalId = (Integer) session.getAttribute("loginHospitalId");
        String sysName = (String) session.getAttribute("loginSysUserName");
        model.addAttribute("sysName", sysName);

        Hospital hospital = hospitalService.findByName(hospitalName);
        model.addAttribute("hospital", hospital);
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.setExcludes(new String[]{"hospital","hospitalPermissionSet","userSet", "masterList"});
        model.addAttribute("hospitalImg", JSONArray.fromObject(hospitalImgService.findByHospital(hospital.getId()), jsonConfig));
        JSONArray jsonArray = hospitalService.findAll();
        jsonArray.remove(0);
        model.addAttribute("hospitals", jsonArray);
        return "images";
    }

    @RequestMapping("/uploadImg")
    @ResponseBody
    public String uploadImg(@RequestParam("file") MultipartFile file, Model model, HttpSession session, @RequestParam Integer imgId) {
        String fileName = new String();
        if (!file.isEmpty() && file != null) {
            try {
                if (imageCheck.isImage(file.getBytes())) {
                    // 获取文件的后缀名
                    fileName = file.getOriginalFilename();
                    String suffixName = fileName.substring(fileName.lastIndexOf("."));
                    //uuid为文件命名
                    fileName = UUID.randomUUID() + suffixName;
                    qiNiuYunUtil.uploadFiles(file.getBytes(), fileName);
                    HospitalImg hospitalImg = hospitalImgService.findById(imgId);
                    hospitalImg.setImgUrl("https://qny.liuxuanlong.xyz/" + fileName);
                    hospitalImgService.updateHospitalImg(hospitalImg);
                    return "上传成功";
                } else {
                    return "不是图片文件";
                }
            } catch (IOException e) {
                e.printStackTrace();
                return "上传失败";
            }
        } else {

            return "文件为空";
        }


    }

    @RequestMapping("/addHospitalImg")
    @ResponseBody
    public String addHospitalImg(@RequestParam("imgFile") MultipartFile file, Model model, HttpSession session, @RequestParam String hospitalNameImg) {
        String fileName = new String();
        if (!file.isEmpty() && file != null) {
            try {
                if (imageCheck.isImage(file.getBytes())) {
                    // 获取文件的后缀名
                    fileName = file.getOriginalFilename();
                    String suffixName = fileName.substring(fileName.lastIndexOf("."));
                    //uuid为文件命名
                    fileName = UUID.randomUUID() + suffixName;
                    qiNiuYunUtil.uploadFiles(file.getBytes(), fileName);
                    Hospital hospital = hospitalService.findByName(hospitalNameImg);
                    HospitalImg hospitalImg = new HospitalImg();
                    hospitalImg.setImgUrl("https://qny.liuxuanlong.xyz/" + fileName);
                    hospitalImg.setHospital(hospital);
                    hospitalImgService.updateHospitalImg(hospitalImg);
                    return "上传成功";
                } else {
                    return "不是图片文件";
                }
            } catch (IOException e) {
                e.printStackTrace();
                return "上传失败";
            }
        } else {

            return "文件为空";
        }


    }
    @RequestMapping("/deleteImg")
    @ResponseBody
    public void deleteImg(@RequestParam Integer deleteHospitalImgId){
        hospitalImgService.deleteImg(deleteHospitalImgId);
    }
}
